﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RecruitmentSystem.Business;
using RecruitmentSystem.Common;

public partial class StudentManagement_UploadPhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindBeginInfo();
        }
    }

    private void BindBeginInfo()
    {
        AccountManager am = new AccountManager();
        PersonalUser pu = (PersonalUser)am.GetUserById(User.Identity.Name, UserType.PersonalUser);
        if (!string.IsNullOrEmpty(pu.HeadPic))
        {
            imgHead.ImageUrl = pu.HeadPic;
        }
    }

    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        string filename = "";
        string HeadPicUrl = "";
        if (FileUpload1.HasFile)
        {
            string size = FileUpload1.PostedFile.ContentLength.ToString();
            double fSize = int.Parse(size) / 1024;
            string fileType = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileType.ToLower() == ".exe")
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "filetypefail", "<script type='text/javascript'>alert('文件格式不对！');</script>");
                return;
            }
            else if (fSize > 50)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "filesizefail", "<script type='text/javascript'>alert('文件大小不能超过50K！');</script>");
                return;
            }
            else
            {
                string savePath = Server.MapPath("../Content/PersonalHead/");
                filename = User.Identity.Name + fileType;
                savePath += filename;
                FileUpload1.SaveAs(savePath);
                HeadPicUrl = ("../Content/PersonalHead/") + filename;
            }
            AccountManager am = new AccountManager();
            PersonalUser pu = (PersonalUser)am.GetUserById(User.Identity.Name, UserType.PersonalUser);
            pu.HeadPic = HeadPicUrl;
            if (am.UpdateUser(pu))
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "success", "<script type='text/javascript'>alert('头像上传成功！');</script>");
                imgHead.ImageUrl = HeadPicUrl;
                return;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "failed", "<script type='text/javascript'>alert('头像上传失败！');</script>");
                return;
            }
        }
        else
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "empty", "<script type='text/javascript'>alert('文件不能为空');</script>");
            return;
        }
    }
}
